package com.zjbti.directory;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText mEtName,mEtPhone;
    private TextView mTvShow;
    private Button mBtnAdd, mBtnQuery, mBtnUpdate, mBtnDelete;
    private SQLiteDatabase db;
    private MyHelper myHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        myHelper=new MyHelper(this);
        db=myHelper.getWritableDatabase();
        init();
    }
    private void init(){
        mEtName=findViewById(R.id.et_name);
        mEtPhone=findViewById(R.id.et_phone);
        mTvShow=findViewById(R.id.tv_show);
        mBtnAdd=findViewById(R.id.btn_add);
        mBtnQuery=findViewById(R.id.btn_query);
        mBtnUpdate=findViewById(R.id.btn_update);
        mBtnDelete=findViewById(R.id.btn_delete);
        mBtnAdd.setOnClickListener(this);
        mBtnQuery.setOnClickListener(this);
        mBtnUpdate.setOnClickListener(this);
        mBtnDelete.setOnClickListener(this);
    }
    private void query(){
        Cursor cursor=db.query("information",null,null,null,null,null,null);
        mTvShow.setText("");
        if(cursor.getCount()==0){
            Toast.makeText(this,"没有数据",Toast.LENGTH_SHORT).show();
        }else {
            while (cursor.moveToNext()){
                mTvShow.append("\n"+"Name : " + cursor.getString(1)+" ;Tel"+cursor.getString(2));
            }
        }
        cursor.close();
    }

    @Override
    public void onClick(View v) {
        String name, phone;
        ContentValues values;
        int viewId = v.getId();
        if (viewId == R.id.btn_add) {
            name = mEtName.getText().toString();
            phone = mEtPhone.getText().toString();
            values = new ContentValues();
            values.put("name", name);
            values.put("phone", phone);
            long id = db.insert("information", null, values);
            if (id != -1) {
                Toast.makeText(this, "信息已添加", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "信息添加失败", Toast.LENGTH_SHORT).show();
            }
        } else if (viewId == R.id.btn_query) {
            query();
        } else if (viewId == R.id.btn_update) {
            Cursor cursor1 = db.query("information", null, null, null, null, null, null);
            if (cursor1.getCount() == 0) {
                Toast.makeText(this, "没有数据可修改", Toast.LENGTH_SHORT).show();
            } else {
                values = new ContentValues();
                values.put("phone", phone = mEtPhone.getText().toString());
                int rows = db.update("information", values, "name=?", new String[]{mEtName.getText().toString()});
                if (rows > 0) {
                    Toast.makeText(this, "信息已修改", Toast.LENGTH_SHORT).show();
                    query();
                } else {
                    Toast.makeText(this, "信息修改失败", Toast.LENGTH_SHORT).show();
                }
            }
        } else if (viewId == R.id.btn_delete) {
            Cursor cursor2 = db.query("information", null, null, null, null, null, null);
            if (cursor2.getCount() == 0) {
                Toast.makeText(this, "没有数据可删除", Toast.LENGTH_SHORT).show();
            } else {
                int delRows = db.delete("information", null, null);
                if (delRows > 0) {
                    Toast.makeText(this, "信息已删除", Toast.LENGTH_SHORT).show();
                    mTvShow.setText("");
                } else {
                    Toast.makeText(this, "信息删除失败", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

    class MyHelper extends SQLiteOpenHelper{

        public MyHelper(@Nullable Context context) {
            super(context, "itcast.db", null, 1);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE information(_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),phone VARCHAR(20))");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        }
    }
}
